<template>
  <div class="content-container">
    <div class="title">✨ 文件上传中心 ✨</div>
    <el-upload
      v-model:file-list="fileList"
      class="custom-upload"
      action="/api/image/upload"
      multiple
      drag
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      :limit="fileLimit"
      :on-exceed="handleExceed"
    >
    <el-icon class="el-icon--upload"><upload-filled /></el-icon>
    <div class="el-upload__text">
      Drop file here or <em>click to upload</em>
    </div>
    <template #tip>
      <div class="el-upload__tip">
        jpg/png files with a size less than 500kb
      </div>
      </template>
    </el-upload>
  </div>


  
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

import type { UploadProps, UploadUserFile } from 'element-plus'

const fileList = ref<UploadUserFile[]>([])
const fileLimit = ref(3);

// 上传文件的格式和大小限制
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg'&& rawFile.type !== 'image/png'&& rawFile.type !== 'image/jpg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}


const isclick = () => {
  console.log('click')
}
const handleRemove: UploadProps['onRemove'] = (file, uploadFiles) => {
  console.log(file, uploadFiles)
}

//造
const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
  console.log(uploadFile)
}

 const handleBeforeUpload = (file) => {
      // 校检文件类型
      console.log(file+"文件的类型");
      
 }
//总的上传数量 UploadProps['onExceed'] files 上传的文件列表 uploadFiles 上传的文件列表
const handleExceed: UploadProps['onExceed'] = (files, uploadFiles) => {
  ElMessage.warning(
  `限制是 ${fileLimit.value} 个文件，您这次选择了 ${files.length} 个文件，总共选择了 ${
    files.length + uploadFiles.length
  } 个文件`
  )
}


const beforeRemove: UploadProps['beforeRemove'] = (uploadFile, uploadFiles) => {
  return ElMessageBox.confirm(
    `Cancel the transfer of ${uploadFile.name} ?`
  ).then(
    () => true,
    () => false
  )
}
</script>

<style scoped>
.content-container {
  max-width: 800px;
  margin: 2rem auto;
  padding: 2.5rem;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  transition: transform 0.3s ease;
}

/* .content-container:hover {
  transform: translateY(-5px);
} */

.title {
  font-size: 2rem;
  color: #2c3e50;
  text-align: center;
  margin-bottom: 2rem;
  font-weight: 600;
  letter-spacing: 1px;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}

.custom-upload {
  :deep(.el-upload) {
    width: 100%;
  }
}

.custom-upload {
  :deep(.el-upload-dragger) {
    padding: 2.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
    border: 2px dashed #4a90e2;
    background: rgba(255,255,255,0.95);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    
    &:hover {
      border-color: #6c5ce7;
      transform: translateY(-2px);
    }

    &.is-dragover {
      border-color: #00b894;
      background: rgba(96, 255, 218, 0.1);
    }
  }
}

.upload-content {
  text-align: center;
}


.upload-btn {
  padding: 12px 30px;
  font-size: 1.1rem;
  background: linear-gradient(45deg, #6c5ce7, #4a90e2);
  border: none;
  border-radius: 8px;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(108,92,231,0.4);
  }
}

.prompt-text {
  color: #7f8c8d;
  margin-top: 1rem;
  font-size: 0.95rem;
}

.custom-tip {
  margin-top: 1.5rem;
  color: #e67e22;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 0.95rem;
}

/* 文件列表美化 */
:deep(.el-upload-list__item) {
  transition: all 0.3s ease;
  border-radius: 8px;
  margin: 8px 0;
  padding: 12px;
  background: rgba(255,255,255,0.9);
  
  &:hover {
    transform: translateX(5px);
    background: rgba(255,255,255,1);
    box-shadow: 0 3px 10px rgba(0,0,0,0.1);
  }
}

:deep(.el-upload-list__item-name) {
  color: #2c3e50;
  font-weight: 500;
}

:deep(.el-icon--close) {
  color: #e74c3c;
  &:hover {
    color: #c0392b;
  }
}

.custom-upload {
  :deep(.el-upload-dragger) {
    padding: 2.5rem;
    border: 2px dashed #4a90e2;
    background: rgba(255,255,255,0.95);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

    &:hover {
      border-color: #6c5ce7;
      background: rgba(255,255,255,1);
      transform: translateY(-2px);
    }

    &.is-dragover {
      border-color: #00b894;
      background: rgba(96, 255, 218, 0.1);
      animation: pulse 1.5s infinite;
    }
  }
}

.upload-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
}

.upload-icon {
  font-size: 3.5rem;
  color: #6c5ce7;
  transition: transform 0.3s ease;
}

.upload-text {
  text-align: center;
  p {
    margin: 0;
    &:first-child {
      font-size: 1.2rem;
      color: #2c3e50;
      font-weight: 500;
    }
  }
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(108,92,231,0.2);
  }
  70% {
    box-shadow: 0 0 0 15px rgba(108,92,231,0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(108,92,231,0);
  }
}

/* 优化拖拽状态提示 */
:deep(.el-upload__dragger) {
  .upload-icon {
    transition: all 0.3s ease;
  }
  
  &.is-dragover {
    .upload-icon {
      transform: scale(1.2);
      color: #00b894;
    }
    p {
      color: #00b894 !important;
    }
  }
}
</style>